#include "ShowfindupdateTMD.h"
#include <algorithm>

bool ShowfindupdateTMD::sort_func(Category a, Category b)
{
  return (a.id_get() < b.id_get());
}

bool ShowfindupdateTMD::is_cat(int new_cat, vector<Category> cats)
{
  int i;
  for(i=0; i < cats.size(); ++i)
  {
	if(cats[i].id_get() == new_cat)
	{
      return true;
	}
  }
  return false;
}

bool ShowfindupdateTMD::is_prod(int prod_id, vector<Product> prods)
{
  int i;
  for(i=0; i < prods.size(); ++i)
  {
    if(prod_id == prods[i].id_get())
	{
	  return true;
	}
  }
  return false;
}

int ShowfindupdateTMD::digits(int a)
{
  int counter = 0;
  while(a != 0)
  {
    a /= 10;
	++ counter;
  }
  return counter;
}

bool ShowfindupdateTMD::two_digits(double d)
{
  int d_3 = int(d * 1000);
  int d_2 = int(d*100)*10;
  int d_r = d_3 - d_2;
  if(d_r == 0)
  {
    return true;
  }
  return false;
}

double ShowfindupdateTMD::get_price(int prod_id, vector<Product> prods)
{
  for(int i = 0; i < prods.size(); ++i)
  {
    if(prod_id == prods[i].id_get())
    {
      return prods[i].price_get();
    }
  }
  cerr << "Invalid product ID not caught when entered. Program Terminated!";
  exit(1);
}

string ShowfindupdateTMD::get_name(int prod_id, vector<Product> prods)
{
  for(int i = 0; i < prods.size(); ++i)
  {
    if(prod_id == prods[i].id_get())
    {
      return prods[i].name_get();
    }
  }
  cerr << "Invalid product ID not caught when entered. Program Terminated!";
  exit(1);
}

void ShowfindupdateTMD::show(vector<Category> cats, vector<Product> prods)
{
    sort(cats.begin(), cats.end(), sort_func);
    cout << "The categories and products:" << endl << endl;
    int i, j;
    for(i=0; i < cats.size(); ++i)
    {
    cout << cats[i].id_get() << " " << cats[i].name_get() << endl;
	cout << endl << "Products:" << endl;
    for(j=0; j < prods.size(); ++j)
    {
    	if(cats[i].id_get() == prods[j].cat_get())
	    {
		cout << setw(6) << prods[j].id_get() << " " << prods[j].name_get() << endl;
	    }
    }
	cout << endl;
    }
}

void ShowfindupdateTMD::find(vector<Cart_item> cart_items, vector<Product> prods)
{
	//ask for product id
	cout << "Please enter a product ID:";
	int prod_id;
	//check that entry is a valid product id
	while(!(cin >> prod_id) || !(is_prod(prod_id, prods)))
	{
	cout << "Invalid product ID. Try Again!" << endl;
	cin.clear();
	cin.ignore();
	cout << "Enter the 6 digit product ID number:";
	prod_id = 0;
	}
	//go to cart data
	//look up product id then quantity
	int i;
	double sum = 0;
	for(i=0; i < cart_items.size(); ++i)
	{
	if(prod_id == cart_items[i].prod_get())
	{
		//find corresponding price
	    //multiply price by quantity
    	int qty = cart_items[i].qty_get();
		double price = get_price(prod_id, prods);
		sum += price * qty;
	}
	}
	//repeat and sum until you get the answer
	cout << "The total sales for " << get_name(prod_id, prods) << " was $" << sum << endl;
}

void ShowfindupdateTMD::update(vector<Category> &cats, vector<Product> &prods)
{
    sort(cats.begin(), cats.end(), sort_func);
    cout << "Here is a list of categories:" << endl;
    int i, j;
    for(i=0; i < cats.size(); ++i)
    {
	cout << cats[i].id_get() << " " << cats[i].name_get() << endl;
    } 
	cout << "Enter the category number of the new product:";
	int new_cat = 0;
	while(!(cin >> new_cat) || !(is_cat(new_cat, cats)))
	{
	cout << "The category number entered was not valid. Try Again!" << endl;
	cout << "The valid category numbers are:" << endl;
	for(i=0; i < cats.size(); ++i)
    {
		cout << cats[i].id_get() << " " << cats[i].name_get() << endl;
    }
	cout << "Enter the category number of the new product:";
	cin.clear();
	cin.ignore();
	}
	cout << "Enter the product name of the new product:";
	string new_name;
	cin >> new_name;
	cout << "Enter the price of " << new_name << ":";
	double new_price;
	while(!(cin >> new_price) || !(two_digits(new_price)))
	{
	cout << "You did not enter a double for the price. Try Again!" << endl;
	cin.clear();
	cin.ignore();
	cout << "Enter the price of " << new_name << ":";
	}
	cout << "Enter the 6 digit product number of " << new_name << endl;
	cout << "If there is less than 6 digits, the computer will simply add leading zeros:";
	int new_id = 0;
	while(!(cin >> new_id) || digits(new_id) > 6 || is_prod(new_id, prods))
	{
	cout << "Invalid product ID. Either you didn't enter a decimal number," << endl;
	cout << "you enterad a number with more than 6 digits," << endl;
	cout << "or you entered an already existing product number. Try Again!" << endl;
	cin.clear();
	cin.ignore();
	cout << "Enter the 6 digit product number of " << new_name << ":";
	new_id = 0;
	}
    Product p = Product(new_id, new_cat, new_name, new_price);
	prods.push_back(p);
}
